/*
 * Copyright 2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beit.mybatis.demo.user.api.controller.v1;

import com.beit.mybatis.demo.common.component.MessageHelper;
import com.beit.mybatis.demo.user.api.dto.UserDTO;
import com.beit.mybatis.demo.user.domain.entity.User;
import com.beit.mybatis.demo.user.domain.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userController.v1")
@RequestMapping("/v1/users")
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    private final UserRepository userRepository;

    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @PostMapping("/")
    public ResponseEntity<List<User>> userQuery() {
        List<User> users = userRepository.list();
        return new ResponseEntity<>(users, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<User> userQueryById(@PathVariable Long id) {
        User user = userRepository.getById(id);
        Assert.notNull(user, MessageHelper.getMessage("demo.warn.userNotFound", null));
        return new ResponseEntity<>(user, HttpStatus.OK);
    }
//
//    @PostMapping("/page")
//    public ResponseEntity<Page<User>> userQueryPage(@RequestBody UserDTO queryDTO,
//                                                    Page<User> pageRequest) {
//        Page<User> result = userRepository.page(pageRequest,
//                new QueryWrapper<User>()
//                        .select(User.FIELD_ID, User.FIELD_AGE, User.FIELD_NAME, User.FIELD_EMAIL)
//                        .ge(queryDTO.getAge() != null, User.FIELD_AGE, queryDTO.getAge()));
//        return new ResponseEntity<>(result, HttpStatus.OK);
//    }
}
